home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Various / DevDisk 65 (1989)(DevWare PD).zip / DevDisk 65 (1989)(DevWare PD).adf / trapsnap / Trap.c < prev    next >
C/C++ Source or Header  |  1990-07-11  |  3KB  |  95 lines

  1. /**           TrapSnapper From The Transactor
  2.  *
  3.  *   This program shows an easy way to handle CPU exceptions from
  4.  *   a C program. Just include the structs below, and put your error
  5.  *   message and/or cleanup code in the function MyHandler(). You can
  6.  *   get the trap number as shown in this example.
  7.  *
  8.  *  (c) 1987 AHA!
  9. **/
  10. #include <exec/tasks.h>
  11.  
  12. struct Task *MyTask, *FindTask();
  13.  
  14. struct TrapData {
  15.      long TrapNum;         /* trap number will be stored here       */
  16.      int (*Code)();        /* pointer to user trap handler function */
  17.      USHORT MLcode[14];    /* trap-handler machine code goes here   */
  18. };
  19.  
  20. struct TrapData MyTrap = {
  21.      0,
  22.      NULL,    /* trap code address will be filled in later */
  23.      0x201F,                 /* MOVE.L  (A7)+,D0        */
  24.      0x41FA, 0xFFF4,         /* LEA     *-$A,A0         */
  25.      0x2080,                 /* MOVE.L  D0,(A0)         */
  26.      0xB0BC, 0x0000, 0x0003, /* CMP.L   #3,D0           */
  27.      0x6202,                 /* BHI.S   *+4             */
  28.      0x504F,                 /* ADDQ.W  #8,A7           */
  29.      0x41FA, 0xFFE8,         /* LEA     *-$16,A0        */
  30.      0x2F50, 0x0002,         /* MOVE.L  (A0),2(A7)      */
  31.      0x4E73                  /* RTE                     */
  32. };
  33.  
  34. /* commented assembler code is below:
  35.  *
  36.  * errnum:    DS.L   1
  37.  * errfunc:   DS.L   1
  38.  * start:     MOVE.L (A7)+,D0      ;pull trap number off stack
  39.  *            LEA    errnum,A0     ;force PC-relative addr mode
  40.  *            MOVE.L D0,(A0)       ;put trap # in errnum for C
  41.  *            CMP.L  #3,D0         ;check for bus or address error
  42.  *            BHI    other         ;other traps (4 or greater)
  43.  *            ADDQ   #8,A7         ;skip extra info on stack
  44.  * other:     LEA    errfunc,A0    ;PC rel addressing
  45.  *            MOVE.L (A0),2(A7)    ;point PC on stack to user routine
  46.  *            RTE                  ;exit from trap-handler
  47. */
  48.  
  49. extern int MyHandler();  /* your trap-handling function */
  50.  
  51. main (argc, argv)
  52. int argc;
  53. char *argv[];
  54. {
  55. int n1, n2;
  56. int i2, *i1;
  57.  
  58.   i1 = 1;
  59.  
  60.   SetTrap();   /* set up trap handler */
  61.  
  62.   if (argc != 2)
  63.   {
  64.      printf("Usage: %s <n>\n", argv[0]);
  65.      exit (0);
  66.   }
  67.   n1 = atoi(argv[1]);  /* value from command line */
  68.  
  69.   /* if arg=1 let's get an address error by referencing an odd address */
  70.   if (n1 == 1)
  71.      i2 = *i1;
  72.  
  73.   /* if n1 is zero, we'll get a divide by zero trap */
  74.   n2 = 100 / n1;
  75.  
  76.   printf("100 divided by %d equals %d\n", n1, n2);
  77. }
  78.  
  79.  
  80. SetTrap ()
  81. {
  82.   MyTask = FindTask(0L);
  83.   MyTask->tc_TrapCode = (APTR)MyTrap.MLcode;
  84.   MyTrap.Code = MyHandler;
  85. }
  86.  
  87.  
  88. MyHandler ()
  89. {
  90.   printf("Hey - watch it! If I weren't a sophisticated\n");
  91.   printf("Transactor program, I would have bombed out!\n");
  92.   printf("(The problem was, I got a trap number %ld)\n", MyTrap.TrapNum);
  93.   exit(0);
  94. }
  95.